package com.linkoog.devtools.utils;

import java.security.MessageDigest;
import java.security.NoSuchAlgorithmException;
import java.util.Base64;

public class MD5Util {


    public static String md5_16(String source){
        final String md5_32 = md5_32(source);
        if (md5_32 != null){
            return md5_32.substring(8, 24);
        }
        return null;
    }

    public static String md5_32(String source){
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(source.getBytes());
            byte b[] = md.digest();
            int i;
            StringBuffer buf = new StringBuffer("");
            for (int offset = 0; offset < b.length; offset++) {
                i = b[offset];
                if (i < 0)
                    i += 256;
                if (i < 16)
                    buf.append("0");
                buf.append(Integer.toHexString(i));
            }
            return buf.toString();
        } catch (NoSuchAlgorithmException e) {
        }
        return null;
    }

    public static String md5_64(String source){
        try {
            MessageDigest md = MessageDigest.getInstance("MD5");
            md.update(source.getBytes());
            byte b[] = md.digest();
            return Base64.getEncoder().encodeToString(b);
        } catch (NoSuchAlgorithmException e) {
        }
        return null;
    }

    public static String SHA1_64(String source){
        try {
            MessageDigest md = MessageDigest.getInstance("SHA-1");
            md.update(source.getBytes());
            byte b[] = md.digest();
            return Base64.getEncoder().encodeToString(b);
        } catch (NoSuchAlgorithmException e) {
        }
        return null;
    }

    public static String SHA256_64(String source){
        try {
            MessageDigest md = MessageDigest.getInstance("SHA-256");
            md.update(source.getBytes());
            byte b[] = md.digest();
            return Base64.getEncoder().encodeToString(b);
        } catch (NoSuchAlgorithmException e) {
        }
        return null;
    }

    public static String SHA512_64(String source){
        try {
            MessageDigest md = MessageDigest.getInstance("SHA-512");
            md.update(source.getBytes());
            byte b[] = md.digest();
            return Base64.getEncoder().encodeToString(b);
        } catch (NoSuchAlgorithmException e) {
        }
        return null;
    }




}
